package com.company.project.util;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpUtil {

    /**
     * http发送get
     * @param targetURL
     * @return
     */
    public static String httpGet(String targetURL){
        StringBuffer ret = new StringBuffer();
        try {
            URL restServiceURL = new URL(targetURL);

            HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
            httpConnection.setRequestMethod("GET");
            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            if (httpConnection.getResponseCode() != 200) {
                throw new RuntimeException("HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode());
            }
            String output;
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((httpConnection.getInputStream()), "UTF-8"));
            while ((output = responseBuffer.readLine()) != null) {
                ret.append(output);
            }
            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret.toString();
    }

    /**
     * httpPost包 发送的post请求
     * @param url
     * @param jstr
     */
    public static String httpPost(String url,String jstr){
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(jstr, HTTP.UTF_8);
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        HttpClient client = new DefaultHttpClient();
        BufferedReader br;
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject responseJsonbody = getResponseJsonbody(response);
        return responseJsonbody.toString();
    }

    public static JSONArray httpPosta(String url,String jstr){
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(jstr, HTTP.UTF_8);
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        HttpClient client = new DefaultHttpClient();
        BufferedReader br;
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONArray responseJsonbody = getResponseJsonbodys(response);
        return responseJsonbody;
    }

    /**
     * HttpDelete 发送的delete请求
     * @param url
     */
    public static String httpDelete(String url){
        HttpDelete httpDelete = new HttpDelete(url);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = null;
        try {
            response = client.execute(httpDelete);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject responseJsonbody = getResponseJsonbody(response);
        return responseJsonbody.toString();
    }

    public static JSONObject getResponseJsonbody(HttpResponse response){
        BufferedReader br;
        JSONObject jsonb = null;
        try {
            HttpEntity entity2 = response.getEntity();
            InputStream content = entity2.getContent();
            br = new BufferedReader(new InputStreamReader(content,"utf-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            String json = sb.toString();
            jsonb = JSONObject.fromObject(json);
            if (jsonb==null){
                JSONArray jsonArray = JSONArray.fromObject(json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonb;
    }
    public static JSONArray getResponseJsonbodys(HttpResponse response){
        BufferedReader br;
        JSONArray jsonArray = null;
        try {
            HttpEntity entity2 = response.getEntity();
            InputStream content = entity2.getContent();
            br = new BufferedReader(new InputStreamReader(content,"utf-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            String json = sb.toString();
            jsonArray = JSONArray.fromObject(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    public static String unicodeToString(String str) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch+"" );
        }
        return str.replaceAll("\\\\","");
    }


}

